﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Threading;
using System.Runtime.InteropServices;

namespace PPTControl
{
    public partial class Form1 : Form
    {
        private int UID = 0;
        private IProxy _proxy = null;
        private WorkerThread _worker = null;
        private Microsoft.Office.Interop.PowerPoint.Application _app;
        private bool _started = false;
        private static MagnifierForm _magnifier = null;
        private delegate void ShowMagnifierCallback();
        private delegate void HideMagnifierCallback();
        private delegate void MoveManigerCallback(Point pt);

        public Form1()
        {
            InitializeComponent();
            _proxy = new HTTPProxy();
        }
        Common.Protocol.LoginReq _loginInfo;
        //ServiceHost host;
        private void Form1_Load(object sender, EventArgs e)
        {
            LoginForm loginForm = new LoginForm(_proxy);
            if (loginForm.ShowDialog() != DialogResult.OK)
            {
                this.Close();
                return;
            }

            UID = loginForm.UID;
            _loginInfo = loginForm.LoginInfo;

           // host = new System.ServiceModel.ServiceHost(typeof(ServiceLayer.Service1));
          //  host.Opened += new EventHandler(host_Opened);
          //  host.Open();
            _app = new Microsoft.Office.Interop.PowerPoint.Application();
           
        }

        private void host_Opened(object sender, EventArgs e)
        {
            this.textBox1.Text = "opened";
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            Logger.Write("resolution:" + Screen.PrimaryScreen.WorkingArea.Width + ":" + Screen.PrimaryScreen.WorkingArea.Height);
            if (!_started)
            {
                Start();
                button1.Text = "Stop";
                _started = true;
            }
            else
            {
                Stop();
                button1.Text = "Start";
                _started = false;
            }
        }
        
        private void Start()
        {
            _worker = new WorkerThread(_app, this, UID, _proxy);
            _worker.LoginInfo = _loginInfo;
        }
    
        private void Stop()
        {
            _worker.Stop();
               
        }

        private void buttonOpenFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            DialogResult ret = ofd.ShowDialog(this);
            if(ret== System.Windows.Forms.DialogResult.OK)
            {
                _app.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
                _app.WindowState = Microsoft.Office.Interop.PowerPoint.PpWindowState.ppWindowMaximized;
                _app.Presentations.Open(ofd.FileName);
            }
        }

        protected override void OnClosed(EventArgs e)
        {
            if (_started)
            {
                if (_worker != null)
                    _worker.Stop();
                if (_app != null)
                {
                    _app.Quit();
                    Marshal.ReleaseComObject(_app);
                    _app = null;
                }
            }
            
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
            base.OnClosed(e);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Thread _holderThread;
            _holderThread = new Thread(new ThreadStart(doWork));
            _holderThread.Start();
        }

        protected void doWork()
        {
            ShowMagnifier();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MagnifierForm magnifier = new MagnifierForm(new Configuration(), Cursor.Position);
            magnifier.Show();
        }
        public void MoveManiger(Point pt)
        {
            if (InvokeRequired)
            {
                MoveManigerCallback  d = new MoveManigerCallback(MoveManiger);
                this.Invoke(d, new object[] {pt });
            }
            else
            {
                if (_magnifier != null)
                {
                    Point newStart = new Point();
                    newStart.X = pt.X - _magnifier.Width/2;
                    newStart.Y = pt.Y - _magnifier.Height/2;
                    _magnifier.Location = newStart;
                    _magnifier.Invalidate();
                }
            }
        }
       
        public void ShowMagnifier()
        {
            if (InvokeRequired)
            {
                ShowMagnifierCallback d = new ShowMagnifierCallback(ShowMagnifier);
                this.Invoke(d, new object[] { });
            }
            else
            {
                if (_magnifier != null)
                {
                    _magnifier.Hide();
                    _magnifier.Close();
                    _magnifier.Dispose();
                }
                _magnifier = new MagnifierForm(new Configuration(), Cursor.Position);
                _magnifier.Show();
            }
        }

        public void HideMagnifier()
        {
            if (InvokeRequired)
            {
                HideMagnifierCallback d = new HideMagnifierCallback(HideMagnifier);
                this.Invoke(d, new object[] { });
            }
            else
            {
                if (_magnifier != null)
                {
                    _magnifier.Hide();
                    _magnifier.Close();
                    _magnifier.Dispose();
                    _magnifier = null;
                }
            }
        }
    }
}
